import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { productList } from '#/api/hm/product';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';
import { useAccess } from '@vben/access';
import { DictEnum } from '@vben/constants';
import { computed } from 'vue';
const { hasAccessByRoles } = useAccess();
const isAdmin = computed(() => {
  return hasAccessByRoles([
    'admin',
    'superadmin',
    'ceo',
    'warehouse-manage',
    'dept-manage',
  ]);
});
export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Select',
    fieldName: 'productType',
    defaultValue: 'CP',
    label: '产品类型',
    componentProps: {
      options: getDictOptions(DictEnum.HM_PRODUCT_TYPE),
    },
  },
  {
    component: 'Select',
    fieldName: 'stockProductId',
    label: '产品',
    componentProps: {
      allowClear: true,
      showSearch: true,
      optionFilterProp: 'label',
    },
    dependencies: {
      async componentProps(value) {
        if (value?.productType) {
          let requestParams = {
            pageNum: 1,
            pageSize: 999,
            status: '0',
            productType: '',
          };
          if (value?.productType === 'CP') {
            requestParams.productType = 'CP';
          } else {
            requestParams.productType = 'ZP';
          }
          const ret = await productList(requestParams);
          const dataSourceOptions = [];
          const transOptions = ret.rows.map((item) => ({
            label: item.productName,
            value: item.productId.toString(),
          }));
          dataSourceOptions.push(...transOptions);
          return { options: dataSourceOptions };
        }
        return {};
      },
      triggerFields: ['productType'],
    },
  },
  {
    component: 'Select',
    fieldName: 'productDept',
    label: '仓库',
    dependencies: {
      show: () => isAdmin.value,
      triggerFields: [''],
    },
  },
];

export const columns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '所属仓库',
    field: 'productDeptName',
  },
  {
    title: '产品名',
    field: 'stockProductName',
  },
  {
    title: '产品类型',
    field: 'productType',
    slots: {
      default: ({ row }) => {
        if (row.productType) {
          return renderDict(row.productType, DictEnum.HM_PRODUCT_TYPE);
        }
        return '-';
      },
    },
  },
  {
    title: '库存数量',
    field: 'stockNum',
    slots: { default: 'stockHis' },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 180,
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    label: '库存id',
    fieldName: 'stockId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '商品类型',
    fieldName: 'productType',
    component: 'RadioGroup',
    defaultValue: 'CP',
    componentProps: {
      options: getDictOptions(DictEnum.HM_PRODUCT_TYPE),
      buttonStyle: 'solid',
      optionType: 'button',
    },
    dependencies: {
      disabled: (values) => values.stockId != undefined,
      triggerFields: ['stockId'],
    },
    rules: 'selectRequired',
  },
  {
    label: '产品',
    fieldName: 'stockProductId',
    component: 'Select',
    rules: 'required',
    dependencies: {
      async componentProps(value) {
        const ret = await productList({
          pageNum: 1,
          pageSize: 999,
          status: '0',
          productType: value.productType,
        });
        const dataSourceOptions = [];
        const transOptions = ret.rows.map((item) => ({
          label: item.productName,
          value: item.productId.toString(),
        }));
        dataSourceOptions.push(...transOptions);
        return {
          options: dataSourceOptions,
          allowClear: true,
          showSearch: true,
          optionFilterProp: 'label',
        };
      },
      disabled: (values) => values.stockId != undefined,
      triggerFields: ['productType', 'stockId'],
    },
  },
  {
    label: '库存数量',
    fieldName: 'stockNum',
    component: 'InputNumber',
    componentProps: {
      min: 0,
      precision: 2,
    },
    rules: 'required',
  },
  {
    label: '所在门店',
    fieldName: 'productDept',
    component: 'Select',
    rules: 'required',
    dependencies: {
      disabled: (values) => values.stockId != undefined,
      triggerFields: ['stockId'],
    },
  },
  {
    label: '备注',
    fieldName: 'remark',
    component: 'Input',
  },
];

// 历史记录column与modalSchema
export const stockHisColumn: VxeGridProps['columns'] = [
  {
    title: '记录类型',
    field: 'changeInfo',
    width: 110,
    slots: {
      default: ({ row }) => {
        const color = row.changeInfo.includes('入库') ? 'green' : 'red';
        return <span style={{ color }}>{row.changeInfo}</span>;
      },
    },
  },
  {
    title: '变更数量',
    field: 'changeNum',
    width: 100,
    slots: {
      default: ({ row }) => {
        const symbol = row.changeInfo.includes('入库') ? '+' : '-';
        const color = row.changeInfo.includes('入库') ? 'green' : 'red';
        return (
          <span style={{ color }}>
            {symbol}
            {row.changeNum}
          </span>
        );
      },
    },
  },
  {
    title: '变更时间',
    field: 'changeTime',
    width: 125,
  },
  {
    title: '出库地',
    field: 'recordStartLocationName',
    width: 150,
  },
  {
    title: '入库地',
    field: 'recordEndLocationName',
    width: 150,
  },
  {
    title: '提货/修改人',
    field: 'operator',
    width: 170,
  },
  {
    title: '备注',
    field: 'remark',
    showOverflow: 'tooltip',
    minWidth: 300,
  },
  {
    title: '证明材料',
    field: 'proof',
    showOverflow: true,
    slots: { default: 'url' },
    width: 110,
  },
];

export const stockHisQuerySchema: FormSchemaGetter = () => [
  {
    component: 'RangePicker',
    componentProps: {
      showTime: false,
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
    },
    fieldName: 'pickUpTime',
    label: '处理时间',
  },
];
